//
//  RadioTableViewCell.m
//
//
//  Created by gynjobs on 2017/8/14.
//  Copyright © 2017年 gynjobs. All rights reserved.
//

#import "RadioTableViewCell.h"

static NSInteger radioCount = 2;

static id getMetaTitleArray(){
    
    static id titleArray = nil;
    titleArray = @[@"无",@"有"];
    return titleArray;
}

@implementation RadioBtnView

-(instancetype)init{
    if(self = [super init]){
        self.checkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.checkBtn.layer.cornerRadius = 7.5;
        self.checkBtn.layer.borderColor = [UIColor colorWithString:UI_COLOR_COMMON_TEXT_COLOR_3].CGColor;
        self.checkBtn.layer.borderWidth = 0.5;
        
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.text = @"有";
        self.titleLabel.font = [UIFont systemFontOfSize:UI_FOND_HOME_CREDIT_CELL_MAIN_1];
        self.titleLabel.textColor = [UIColor colorWithString:UI_COLOR_COMMON_TEXT_COLOR_3];
        
        self.tipImageView = [[UIImageView alloc]init];
        self.tipImageView.backgroundColor = [UIColor colorWithString:UI_COLOR_MAIN_BG_3];
        self.tipImageView.layer.cornerRadius = 4;
        self.tipImageView.hidden = YES;
        
        [self addSubview:self.checkBtn];
        [self addSubview:self.titleLabel];
        [self addSubview:self.tipImageView];
    }
    
    [self updateConstraintsIfNeeded];
    return self;
    
}


-(void)updateConstraints{
    [super updateConstraints];
    
    [self.checkBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.mas_centerY);
        make.left.equalTo(self.mas_left);
        make.height.mas_equalTo(RADIO_BTN_HEIGHT);
        make.width.mas_equalTo(RADIO_BTN_HEIGHT);
        
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.mas_centerY);
        make.left.equalTo(self.checkBtn.mas_right).with.offset(TITLE_LABEL_LEFT);
        
    }];
    
    [self.tipImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.checkBtn.mas_centerY);
        make.centerX.equalTo(self.checkBtn.mas_centerX);
        make.width.mas_equalTo(TIP_IMAGEVIEW_HEIGHT);
        make.height.mas_equalTo(TIP_IMAGEVIEW_HEIGHT);
        
    }];
    
    
}


@end


@implementation RadioTableViewCell





-(instancetype)init{
    
    if(self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:RADIO_CELL_ID]){
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.radioArray = [NSMutableArray arrayWithCapacity:2];
        self.radioDelegate = nil;
        self.item = nil;
        [self initRadios];
    }
    
    [self updateConstraintsIfNeeded];
    return self;
    
}

-(void)updateConstraints{
    
    [super updateConstraints];
    [self updateRadios];
    
}

#pragma mark -
#pragma mark init subview

-(void)initRadios{
    
    for (NSInteger i = 0; i < radioCount; i++) {
        
        RadioBtnView * radio = [[RadioBtnView alloc] init];
        [self addSubview:radio];
        [self.radioArray addObject:radio];
        
        NSString * title = [getMetaTitleArray() objectAtIndex:i];
        radio.titleLabel.text = title;
        
        [radio.checkBtn addTarget:self action:@selector(radioBtnPressed:) forControlEvents:UIControlEventTouchDown];
        
    }
    
    //[self setRadioSelected:0];
    
}

-(void)setRadioSelected:(NSInteger)index{
    
    for (NSInteger i = 0; i < [self.radioArray count]; i ++) {
        RadioBtnView * radio = [self.radioArray objectAtIndex:i];
        radio.tipImageView.hidden = YES;
        
        if(i == index){
            
            radio.tipImageView.hidden = NO;
        }
        
    }
    
}

#pragma mark -
#pragma mark update subview

-(void)updateRadios{
    
    id preCons = self.mas_right;
    
    
    for (NSInteger i = 0; i < [self.radioArray count]; i ++) {
        
        RadioBtnView *radio = [self.radioArray objectAtIndex:i];
        
        [radio mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.centerY.equalTo(self.mas_centerY);
            make.right.equalTo(preCons).with.offset(-LEFT_RIGHT_SPACE);
            make.height.mas_equalTo(RADIO_BTN_HEIGHT);
            make.width.mas_equalTo(RADIO_BTN_WIDTH);
            
        }];
        
        preCons = radio.mas_left;
        
    }
    
    
}


#pragma mark -
#pragma mark readio btn pressed
-(void)radioBtnPressed:(id)sender{
    
    NSInteger index = [self.radioArray indexOfObject:[sender superview]];
    index = index;
    
    [self setRadioSelected:index];
    
    if(self.radioDelegate && [self.radioDelegate respondsToSelector:@selector(loanRadioCell:withData:)]){
        
        NSMutableDictionary * dic = [NSMutableDictionary dictionaryWithCapacity:2];
        NSString * indexStr = [NSString stringWithFormat:@"%ld",(long)index];
        
        if(index){
            
            [dic setValue:self.item.tip3 forKey:@"score"];
            
        }else{
            
            [dic setValue:@"0" forKey:@"score"];
            
        }
        
        [dic setValue:self.item.key forKey:@"key"];
        [dic setValue:indexStr forKey:@"data"];
        
        [self.radioDelegate loanRadioCell:self withData:dic];
    }
    
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [self.superview endEditing:YES];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    if ([self.item.key isEqualToString:@"free_insurance"]) {
        GYNLog(@"/n-->>>>>>>>>>>>>/n");
        [self setRadioSelected:1];
    }
    
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
